
import { _cos, _sin, _k, _move, _o, _mid, _lerp } from "../math/index.js";
import { controlPoints } from './controlPoints.js'
import { _traversal } from "./traversal.js";

const _koch = ([from, to], depth, skew, amplitude) => {
    let [c1, c2] = controlPoints([from, to], skew, amplitude)
    let p1 = _lerp(from, to, 1 / 3)
    let p2 = _lerp(from, to, 2 / 3)
    let ps = [from, p1, c1, p2, to]
    if (depth === 0) {
        return [from]
    } else if (depth === 1) {
        return ps
    } else {
        depth -= 1
        let ps2 = [];
        ps.forEach((t, i) => {
            let next = ps[i + 1]
            if (next) {
                let arr = _koch([t, next], depth, skew, amplitude)
                if (i === 0) {
                    ps2.push(...arr)
                } else {
                    ps2.push(...arr.slice(1))
                }

            }
        })
        return ps2
    }
}
export const kochPoints = ({ points, depth = 0, skew = 0, amplitude = 1, loop = false, discrete = false }) => {
    let len = points.length
    let result = []
    let iter = ([t, next], i) => {
        let ps = _koch([t, next], depth, skew, amplitude)
        if (i === 0) {
            result.push(...ps)
        } else if (i === len - 1) {
            result.push(...ps.slice(1, ps.length - 1))
        }
        else {
            result.push(...ps.slice(1))
        }
    }
    let n = 2
    _traversal({ points, n, loop, iter, discrete })

    return result
}